package com.linshengjian.snowball.core;

import com.linshengjian.snowball.core.exception.DataManagerException;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据管理器
 */
public class DataManager {

    private Map<Class, DataWrapper> manages = new HashMap<>();

    /**
     * 创建数据包装器
     * @param type
     * @param handler
     * @return
     */
    public DataWrapper create(Class type, IDataHandler handler) throws DataManagerException {
        if (manages.containsKey(type))
            throw new DataManagerException(type.getName() + " 重复创建");

        DataWrapper wrapper = new DataWrapper(type, handler);
        manages.put(type, wrapper);

        return wrapper;
    }

    /**
     * 获取数据包装器
     * @param type
     * @return
     */
    public DataWrapper get(Class type) {
        return manages.get(type);
    }
}
